package com.ambimmort.shj.action.policy;

import com.ambimmort.shj.service.policy.PolicyService;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * Created by Josh on 2015/7/30.
 */
@Controller("/action/policy/list.do")
public class List {
    @Autowired
    private PolicyService policyService;

    /**
     *
     * @param request
     * @param type
     * @param instance
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("/action/policy/list.do")
    @ResponseBody
    public String get_policy(HttpServletRequest request, @RequestParam("type") String type,@RequestParam("instance") String instance) throws UnsupportedEncodingException {

        System.out.println("---------------------------" + request.getParameter("instance"));
        byte [] instanceArr=Base64.decodeBase64(instance);
        String instances = new String(instanceArr,"utf-8") ;//创建策略仓库实例名称
        System.out.println("instance" + instances);
        try {
            return policyService.list(type,instances);
        }
        catch (Exception e) {
            e.printStackTrace();
            return "{\"result\":\"false\"}";
        }
    }
}
